package com.bci.pwtz.admin.service.impl;

import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;

import com.bci.pwtz.admin.service.AdminDictionaryService;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.DictionaryMapper;
import com.bci.pwtz.mysql.dao.DictionaryTypeMapper;
import com.bci.pwtz.mysql.model.Dictionary;
import com.bci.pwtz.mysql.model.DictionaryType;

public class AdminDictionaryServiceImpl implements AdminDictionaryService {

	private DictionaryMapper dictionaryMapper;
	private DictionaryTypeMapper dictionaryTypeMapper;
	private static final Logger _logger = Logger.getLogger(AdminDictionaryServiceImpl.class);
	
	public void setDictionaryMapper(DictionaryMapper dictionaryMapper) {
		this.dictionaryMapper = dictionaryMapper;
	}

	public void setDictionaryTypeMapper(DictionaryTypeMapper dictionaryTypeMapper) {
		this.dictionaryTypeMapper = dictionaryTypeMapper;
	}

	@Override
	public List<DictionaryType> selectDictionaryType()
			throws PwtzDatabaseException {
		try {
			return dictionaryTypeMapper.selectDictionaryType();
		} catch (DataAccessException e) {
			_logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
		}
	}

	@Override
	public List<DictionaryType> selectDictionaryTypeByRoot()
			throws PwtzDatabaseException {
		try {
			return dictionaryTypeMapper.selectByRoot();
		} catch (DataAccessException e) {
			_logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
		}
	}

	@Override
	public List<DictionaryType> selectDictionaryTypeByParent(Long pid)
			throws PwtzDatabaseException {
		try {
			return dictionaryTypeMapper.selectByParent(pid);
		} catch (DataAccessException e) {
			_logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
		}
	}

	@Override
	public PaginationList<Dictionary> findDictionary(Long pid, Long deviceType,Integer status,
			int pageSize, int pageNo) throws PwtzDatabaseException {
		try {
			int beginIndex = pageSize * pageNo - pageSize;
			List<Dictionary> list = dictionaryMapper.selectByDeviceList(pid, deviceType,status, beginIndex, pageSize);
			_logger.debug("DictionaryServiceImpl.findDictionary() result list = " + list);
			int totalCount = dictionaryMapper.selectByDeviceCount(pid, deviceType,status);
			_logger.debug("DictionaryServiceImpl.findDictionary() result totalCount = " + totalCount);
			
			int totalPage = (totalCount + pageSize -1)/pageSize;
			
			PaginationList<Dictionary> paginationList = new PaginationList<Dictionary>();
			paginationList.setTotalPage(totalPage);
			paginationList.setCurrPage(pageNo);
			paginationList.setPageSize(pageSize);
			paginationList.setRecords(list);
			paginationList.setTotalRecord(totalCount);
			return paginationList;
		} catch (DataAccessException e) {
			_logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
		}
	}

	@Override
	public int approveDictionary(Long id,int status) throws PwtzDatabaseException {
		try {
			if(status == 1){
				Dictionary bean = new Dictionary();
				bean.setDictionaryId(id);
				bean.setStatus(1);
				return dictionaryMapper.updateByPrimaryKeySelective(bean);
			}else if(status == 2){
				return dictionaryMapper.deleteByPrimaryKey(id);
			}
			return 0;
		} catch (DataAccessException e) {
			_logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
		}
	}

	@Override
	public Dictionary load(Long id) throws PwtzDatabaseException {
		try {
			return dictionaryMapper.selectByPrimaryKey(id);
		} catch (DataAccessException e) {
			_logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
		}
	}

	@Override
	public Dictionary add(Dictionary bean) throws PwtzDatabaseException {
		try {
			dictionaryMapper.insertSelective(bean);
			return bean;
		} catch (DataAccessException e) {
			_logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
		}
	}

	@Override
	public int update(Dictionary bean) throws PwtzDatabaseException {
		try {
			return dictionaryMapper.updateByPrimaryKeySelective(bean);
		} catch (DataAccessException e) {
			_logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
		}
	}

	@Override
	public int delete(Long id) throws PwtzDatabaseException {
		try {
			return dictionaryMapper.deleteByPrimaryKey(id);
		} catch (DataAccessException e) {
			_logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
		}
	}

}
